<?php
namespace app\channel\controller;
use think\Db;

class RebateList extends Base
{
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $gameId = input('game_id');
            $roleName = input('role_name');

            session('admin.cps_id') && $where['cps_id']=session('admin.cps_id');
            session('admin.sdk_id') && $where['channel_id']=session('admin.sdk_id');
            $gameId && $where['game_id'] = $gameId;
            $roleName && $where['role_name'] = ['like', "%{$roleName}%"];
            if( ! $count=Db::name('ChannelRebate')->where($where)->count() ){
                $this->layTable();
            }

            $select = Db::name('ChannelRebate')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('apply_time desc')
                ->select();

            $data = [];
            $gameIdName = model('manage/Game')->gameIdName();
            $channelRebateStatus= config('channel_rebate_status');
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'apply_time' => $v['apply_time'] ? date('Y-m-d H:i:s', $v['apply_time']) : '',
                    'game_id' => $v['game_id'],
                    'game_name' => isset($gameIdName[$v['game_id']]) ? $gameIdName[$v['game_id']] : $v['game_name'],
                    'server_name' => $v['server_name'],
                    'role_name' => $v['role_name'],
                    'role_id' => $v['role_id'],
                    'pay_money' => $v['pay_money'],
                    'pay_time' => $v['pay_time'] ? date('Y-m-d', $v['pay_time']) : '',
                    'pay_id' => $v['pay_id'],
                    'remark' => $v['remark'],
                    'reason' => $v['reason'],
                    'message' => $v['message'],
                    'status' => $channelRebateStatus[ $v['status'] ],
                ];
            }

            $this->layTable(0, '已返利列表', $data, $count);
        }else{
            $gameData = model('Base')->gameNotice();
            return view('', ['game_data'=>$gameData]);
        }
    }

    public function apply(){
        if(request()->isAjax()){
            $gameId = input('game_id');
            $serverName = input('server_name');
            $roleName = input('role_name');
            $roleId = input('role_id');
            $payMoney = input('pay_money');
            $payTime = input('pay_time');
            $payId = input('pay_id');
            $remark = input('remark');
            $cpsId = session('admin.cps_id') ? session('admin.cps_id') : '';
            $sdkId = session('admin.sdk_id') ? session('admin.sdk_id') : '';

            if(!$startTime = strtotime($payTime)){
                $this->layJson(1, '充值时间格式错误');
            }

            $save = [
                'game_id' => $gameId,
                'server_name' => $serverName,
                'role_name' => $roleName,
                'role_id' => $roleId,
                'pay_money' => $payMoney,
                'pay_time' => $startTime,
                'pay_id' => $payId,
                'remark' => $remark,
                'cps_id' => $cpsId,
                'channel_id' => $sdkId,
                'apply_time' => time(),
                'status' => 0
            ];
            $addRs = model('ChannelRebate')->save($save);
            //验证该返利信息是否归属于该渠道
            /*
            $endTime = $startTime+86399;

            $where['game_id'] = $gameId;
            $where['role_name'] = $roleName;
            $where['total_price'] = $payMoney*100;
            $where['pay_time'] = ['between', [$startTime, $endTime]];

            if($cpsId && $sdkId){
                $where['channel_id'] = 1000;
                $where['reg_cps_id'] = $cpsId;
                if( Db::name('PayOrderY_'.date('Y', $startTime))->where($where)->count() ){
                    model('ChannelRebate')->save($save);
                    $this->layJson(0, '返利申请成功，请等待客服人员处理');
                }

                $where['channel_id'] = $sdkId;
                unset($where['reg_cps_id']);
                if( Db::name('PayOrderY_'.date('Y', $startTime))->where($where)->count() ){
                    model('ChannelRebate')->save($save);
                    $this->layJson(0, '返利申请成功，请等待客服人员处理');
                }
            }else if($cpsId){
                $where['channel_id'] = 1000;
                $where['reg_cps_id'] = $cpsId;
                if( Db::name('PayOrderY_'.date('Y', $startTime))->where($where)->count() ){
                    model('ChannelRebate')->save($save);
                    $this->layJson(0, '返利申请成功，请等待客服人员处理');
                }
            }else if($sdkId){
                $where['channel_id'] = $sdkId;
                if( Db::name('PayOrderY_'.date('Y', $startTime))->where($where)->count() ){
                    model('ChannelRebate')->save($save);
                    $this->layJson(0, '返利申请成功，请等待客服人员处理');
                }
            }*/

            $addRs ? $this->layJson(0, '返利申请成功，请等待客服人员处理') : $this->layTable(1, model('ChannelRebate')->getError());
        }else{
            $gameData = model('Base')->gameNotice();
            return view('', ['game_data'=>$gameData]);
        }
    }

    //修改备注
    public function remark(){
        $remark = input('remark');
        $id = input('id');
        !$remark && $this->layJson(1, '请填写备注');

        $saveRs = model('ChannelRebate')->save(['remark'=>$remark], ['id'=>$id]);
        $saveRs!==false ? $this->layJson(0, '提交成功', $remark) : $this->layJson(1, '填写失败，请重试');
    }
}